home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 December / december_2000.iso / Intercd / root / Multimedia / audio / ^NoiseTracker / NtkSourceCode / aiff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-30  |  11.2 KB  |  396 lines

  1. /*
  2.  * September 25, 1991
  3.  * Copyright 1991 Guido van Rossum And Sundry Contributors
  4.  * This source code is freely redistributable and may be used for
  5.  * any purpose.  This copyright notice must be maintained. 
  6.  * Guido van Rossum And Sundry Contributors are not responsible for 
  7.  * the consequences of using this software.
  8.  */
  9.  
  10. /*
  11.  * Sound Tools SGI/Amiga AIFF format.
  12.  * Used by SGI on 4D/35 and Indigo.
  13.  * This is also part of the IFF format used by the Amiga.
  14.  */
  15.  
  16. #include <math.h>
  17. #include "st.h"
  18.  
  19. /* Private data used by writer */
  20. struct aiffpriv {
  21.     unsigned long nsamples;
  22. };
  23.  
  24. double read_ieee_extended();
  25.  
  26. aiffstartread(ft) 
  27. ft_t ft;
  28. {
  29.     char buf[4];
  30.     unsigned long totalsize;
  31.     unsigned long chunksize;
  32.     int channels;
  33.     unsigned long frames;
  34.     int bits;
  35.     double rate;
  36.     unsigned long offset;
  37.     unsigned long blocksize;
  38.     int littlendian = 0;
  39.     char *endptr;
  40.  
  41.     /* FORM chunk */
  42.     if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "FORM", 4) != 0)
  43.         fail("AIFF header does not begin with magic word 'FORM'");
  44.     totalsize = rblong(ft);
  45.     if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "AIFF", 4) != 0)
  46.         fail("AIFF 'FORM' chunk does not specify 'AIFF' as type");
  47.  
  48.     /* This is not completely general; other chunk types may be present
  49.        in AIFF files; but this is all I care about for now. */
  50.  
  51.     /* COMM chunk */
  52.     if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "COMM", 4) != 0)
  53.         fail("AIFF header doesn't have 'COMM' chunk");
  54.     chunksize = rblong(ft);
  55.     if (chunksize != 18)
  56.         fail("AIFF COMM chunk has bad size");
  57.     channels = rbshort(ft);
  58.     frames = rblong(ft);
  59.     bits = rbshort(ft);
  60.     rate = read_ieee_extended(ft);
  61.  
  62.     /* SSND chunk */
  63.     if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "SSND", 4) != 0)
  64.         fail("AIFF header doesn't have 'SSND' chunk");
  65.     chunksize = rblong(ft);
  66.     offset = rblong(ft);
  67.     blocksize = rblong(ft);
  68.     if (blocksize != 0)
  69.         fail("AIFF header specifies nonzero blocksize?!?!");
  70.     while (offset > 0) {
  71.         if (getc(ft->fp) == EOF)
  72.             fail("EOF in AIFF file while skipping header");
  73.     }
  74.  
  75.     ft->info.channels = channels;
  76.     ft->info.rate = rate;
  77.     ft->info.style = SIGN2;
  78.     switch (bits) {
  79.     case 8:
  80.         ft->info.size = BYTE;
  81.         break;
  82.     case 16:
  83.         ft->info.size = WORD;
  84.         break;
  85.     default:
  86.         fail("unsupported sample size in AIFF header");
  87.         /*NOTREACHED*/
  88.     }
  89.     endptr = (char *) &littlendian;
  90.     *endptr = 1;
  91.     if (littlendian == 1)
  92.         ft->swap = 1;
  93. }
  94.  
  95. /* When writing, the header is supposed to contain the number of
  96.    samples and data bytes written.
  97.    Since we don't know how many samples there are until we're done,
  98.    we first write the header with an very large number,
  99.    and at the end we rewind the file and write the header again
  100.    with the right number.  This only works if the file is seekable;
  101.    if it is not, the very large size remains in the header.
  102.    Strictly spoken this is not legal, but the playaiff utility
  103.    will still be able to play the resulting file. */
  104.  
  105. aiffstartwrite(ft)
  106. ft_t ft;
  107. {
  108.     struct aiffpriv *p = (struct aiffpriv *) ft->priv;
  109.     int littlendian = 0;
  110.     char *endptr;
  111.  
  112.     p->nsamples = 0;
  113.     if (ft->info.style == ULAW && ft->info.size == BYTE) {
  114.         report("expanding 8-bit u-law to 16 bits");
  115.         ft->info.size = WORD;
  116.     }
  117.     ft->info.style = SIGN2; /* We have a fixed style */
  118.     /* Compute the "very large number" so that a maximum number
  119.        of samples can be transmitted through a pipe without the
  120.        risk of causing overflow when calculating the number of bytes.
  121.        At 48 kHz, 16 bits stereo, this gives ~3 hours of music.
  122.        Sorry, the AIFF format does not provide for an "infinite"
  123.        number of samples. */
  124.     aiffwriteheader(ft, 0x7f000000 / (ft->info.size*ft->info.channels));
  125.  
  126.     endptr = (char *) &littlendian;
  127.     *endptr = 1;
  128.     if (littlendian == 1)
  129.         ft->swap = 1;
  130. }
  131.  
  132. aiffwrite(ft, buf, len)
  133. ft_t ft;
  134. long *buf, len;
  135. {
  136.     struct aiffpriv *p = (struct aiffpriv *) ft->priv;
  137.     p->nsamples += len;
  138.     rawwrite(ft, buf, len);
  139. }
  140.  
  141. void
  142. aiffstopwrite(ft)
  143. ft_t ft;
  144. {
  145.     struct aiffpriv *p = (struct aiffpriv *) ft->priv;
  146.     if (!ft->seekable)
  147.         return;
  148.     if (fseek(ft->fp, 0L, 0) != 0)
  149.         fail("can't rewind output file to rewrite AIFF header");
  150.     aiffwriteheader(ft, p->nsamples / ft->info.channels);
  151. }
  152.  
  153. aiffwriteheader(ft, nframes)
  154. ft_t ft;
  155. long nframes;
  156. {
  157.     int hsize =
  158.         8 /*COMM hdr*/ + 18 /*COMM chunk*/ +
  159.         8 /*SSND hdr*/ + 12 /*SSND chunk*/;
  160.     int bits;
  161.  
  162.     if (ft->info.style == SIGN2 && ft->info.size == BYTE)
  163.         bits = 8;
  164.     else if (ft->info.style == SIGN2 && ft->info.size == WORD)
  165.         bits = 16;
  166.     else
  167.         fail("unsupported output style/size for AIFF header");
  168.  
  169.     fputs("FORM", ft->fp); /* IFF header */
  170.     wblong(ft, hsize + nframes * ft->info.size * ft->info.channels); /* file 
  171. size */
  172.     fputs("AIFF", ft->fp); /* File type */
  173.  
  174.     /* COMM chunk -- describes encoding (and #frames) */
  175.     fputs("COMM", ft->fp);
  176.     wblong(ft, (long) 18); /* COMM chunk size */
  177.     wbshort(ft, ft->info.channels); /* nchannels */
  178.     wblong(ft, nframes); /* number of frames */
  179.     wbshort(ft, bits); /* sample width, in bits */
  180.     write_ieee_extended(ft, (double)ft->info.rate);
  181.  
  182.     /* SSND chunk -- describes data */
  183.     fputs("SSND", ft->fp);
  184.     wblong(ft, 8 + nframes * ft->info.channels * ft->info.size); /* chunk size 
  185. */
  186.     wblong(ft, (long) 0); /* offset */
  187.     wblong(ft, (long) 0); /* block size */
  188. }
  189.  
  190. double ConvertFromIeeeExtended();
  191.  
  192. double read_ieee_extended(ft)
  193. ft_t ft;
  194. {
  195.     char buf[10];
  196.     if (fread(buf, 1, 10, ft->fp) != 10)
  197.         fail("EOF while reading IEEE extended number");
  198.     return ConvertFromIeeeExtended(buf);
  199. }
  200.  
  201. write_ieee_extended(ft, x)
  202. ft_t ft;
  203. double x;
  204. {
  205.     char buf[10];
  206.     ConvertToIeeeExtended(x, buf);
  207.     /*
  208.     report("converted %g to %o %o %o %o %o %o %o %o %o %o",
  209.         x,
  210.         buf[0], buf[1], buf[2], buf[3], buf[4],
  211.         buf[5], buf[6], buf[7], buf[8], buf[9]);
  212.     */
  213.     (void) fwrite(buf, 1, 10, ft->fp);
  214. }
  215.  
  216.  
  217. /*
  218.  * C O N V E R T   T O   I E E E   E X T E N D E D
  219.  */
  220.  
  221. /* Copyright (C) 1988-1991 Apple Computer, Inc.
  222.  * All rights reserved.
  223.  *
  224.  * Machine-independent I/O routines for IEEE floating-point numbers.
  225.  *
  226.  * NaN's and infinities are converted to HUGE_VAL or HUGE, which
  227.  * happens to be infinity on IEEE machines.  Unfortunately, it is
  228.  * impossible to preserve NaN's in a machine-independent way.
  229.  * Infinities are, however, preserved on IEEE machines.
  230.  *
  231.  * These routines have been tested on the following machines:
  232.  *    Apple Macintosh, MPW 3.1 C compiler
  233.  *    Apple Macintosh, THINK C compiler
  234.  *    Silicon Graphics IRIS, MIPS compiler
  235.  *    Cray X/MP and Y/MP
  236.  *    Digital Equipment VAX
  237.  *
  238.  *
  239.  * Implemented by Malcolm Slaney and Ken Turkowski.
  240.  *
  241.  * Malcolm Slaney contributions during 1988-1990 include big- and little-
  242.  * endian file I/O, conversion to and from Motorola's extended 80-bit
  243.  * floating-point format, and conversions to and from IEEE single-
  244.  * precision floating-point format.
  245.  *
  246.  * In 1991, Ken Turkowski implemented the conversions to and from
  247.  * IEEE double-precision format, added more precision to the extended
  248.  * conversions, and accommodated conversions involving +/- infinity,
  249.  * NaN's, and denormalized numbers.
  250.  */
  251.  
  252. #ifndef HUGE_VAL
  253. # define HUGE_VAL HUGE
  254. #endif /*HUGE_VAL*/
  255.  
  256. # define FloatToUnsigned(f)      ((unsigned long)(((long)(f - 2147483648.0)) + 
  257. 2147483647L + 1))
  258.  
  259. ConvertToIeeeExtended(num, bytes)
  260. double num;
  261. char *bytes;
  262. {
  263.     int    sign;
  264.     int expon;
  265.     double fMant, fsMant;
  266.     unsigned long hiMant, loMant;
  267.  
  268.     if (num < 0) {
  269.         sign = 0x8000;
  270.         num *= -1;
  271.     } else {
  272.         sign = 0;
  273.     }
  274.  
  275.     if (num == 0) {
  276.         expon = 0; hiMant = 0; loMant = 0;
  277.     }
  278.     else {
  279.         fMant = frexp(num, &expon);
  280.         if ((expon > 16384) || !(fMant < 1)) {    /* Infinity or NaN */
  281.             expon = sign|0x7FFF; hiMant = 0; loMant = 0; /* infinity */
  282.         }
  283.         else {    /* Finite */
  284.             expon += 16382;
  285.             if (expon < 0) {    /* denormalized */
  286.                 fMant = ldexp(fMant, expon);
  287.                 expon = 0;
  288.             }
  289.             expon |= sign;
  290.             fMant = ldexp(fMant, 32);          
  291.             fsMant = floor(fMant); 
  292.             hiMant = FloatToUnsigned(fsMant);
  293.             fMant = ldexp(fMant - fsMant, 32); 
  294.             fsMant = floor(fMant); 
  295.             loMant = FloatToUnsigned(fsMant);
  296.         }
  297.     }
  298.     
  299.     bytes[0] = expon >> 8;
  300.     bytes[1] = expon;
  301.     bytes[2] = hiMant >> 24;
  302.     bytes[3] = hiMant >> 16;
  303.     bytes[4] = hiMant >> 8;
  304.     bytes[5] = hiMant;
  305.     bytes[6] = loMant >> 24;
  306.     bytes[7] = loMant >> 16;
  307.     bytes[8] = loMant >> 8;
  308.     bytes[9] = loMant;
  309. }
  310.  
  311.  
  312. /*
  313.  * C O N V E R T   F R O M   I E E E   E X T E N D E D  
  314.  */
  315.  
  316. /* 
  317.  * Copyright (C) 1988-1991 Apple Computer, Inc.
  318.  * All rights reserved.
  319.  *
  320.  * Machine-independent I/O routines for IEEE floating-point numbers.
  321.  *
  322.  * NaN's and infinities are converted to HUGE_VAL or HUGE, which
  323.  * happens to be infinity on IEEE machines.  Unfortunately, it is
  324.  * impossible to preserve NaN's in a machine-independent way.
  325.  * Infinities are, however, preserved on IEEE machines.
  326.  *
  327.  * These routines have been tested on the following machines:
  328.  *    Apple Macintosh, MPW 3.1 C compiler
  329.  *    Apple Macintosh, THINK C compiler
  330.  *    Silicon Graphics IRIS, MIPS compiler
  331.  *    Cray X/MP and Y/MP
  332.  *    Digital Equipment VAX
  333.  *
  334.  *
  335.  * Implemented by Malcolm Slaney and Ken Turkowski.
  336.  *
  337.  * Malcolm Slaney contributions during 1988-1990 include big- and little-
  338.  * endian file I/O, conversion to and from Motorola's extended 80-bit
  339.  * floating-point format, and conversions to and from IEEE single-
  340.  * precision floating-point format.
  341.  *
  342.  * In 1991, Ken Turkowski implemented the conversions to and from
  343.  * IEEE double-precision format, added more precision to the extended
  344.  * conversions, and accommodated conversions involving +/- infinity,
  345.  * NaN's, and denormalized numbers.
  346.  */
  347.  
  348. #ifndef HUGE_VAL
  349. # define HUGE_VAL HUGE
  350. #endif /*HUGE_VAL*/
  351.  
  352. # define UnsignedToFloat(u)         (((double)((long)(u - 2147483647L - 1))) + 
  353. 2147483648.0)
  354.  
  355. /****************************************************************
  356.  * Extended precision IEEE floating-point conversion routine.
  357.  ****************************************************************/
  358.  
  359. double ConvertFromIeeeExtended(bytes)
  360. unsigned char *bytes;    /* LCN */
  361. {
  362.     double    f;
  363.     int    expon;
  364.     unsigned long hiMant, loMant;
  365.     
  366.     expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
  367.     hiMant    =    ((unsigned long)(bytes[2] & 0xFF) << 24)
  368.             |    ((unsigned long)(bytes[3] & 0xFF) << 16)
  369.             |    ((unsigned long)(bytes[4] & 0xFF) << 8)
  370.             |    ((unsigned long)(bytes[5] & 0xFF));
  371.     loMant    =    ((unsigned long)(bytes[6] & 0xFF) << 24)
  372.             |    ((unsigned long)(bytes[7] & 0xFF) << 16)
  373.             |    ((unsigned long)(bytes[8] & 0xFF) << 8)
  374.             |    ((unsigned long)(bytes[9] & 0xFF));
  375.  
  376.     if (expon == 0 && hiMant == 0 && loMant == 0) {
  377.         f = 0;
  378.     }
  379.     else {
  380.         if (expon == 0x7FFF) {    /* Infinity or NaN */
  381.             f = HUGE_VAL;
  382.         }
  383.         else {
  384.             expon -= 16383;
  385.             f  = ldexp(UnsignedToFloat(hiMant), expon-=31);
  386.             f += ldexp(UnsignedToFloat(loMant), expon-=32);
  387.         }
  388.     }
  389.  
  390.     if (bytes[0] & 0x80)
  391.         return -f;
  392.     else
  393.         return f;
  394. }
  395.  
  396.